package VIEWS;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import CONTROLLERS.LuongControllers;
import ModelEntityFW.LuongEntity;
import ModelEntityFW.UserEntitys;
import javax.swing.JToolBar;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Editluong extends JFrame {

	private JPanel contentPane;
	private static Connection m_objConnection;
    private LuongControllers m_objLuongController;
    private static UserEntitys m_objSecurityContext;
    private LuongEntity m_LuongEntity;
    private JTextField lCoBan;
    private JTextField lThuong;
    private JTextField tongLuong;
    private JTextField MaNhanVien;
    private JLabel HoVaTen;
    private JLabel TrinhDo;
    private JLabel ngayLamViec;
    private JTextField txtHoVaTen;
    private JTextField txtTrinhDo;
    private JTextField txtNgayLamViec;

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Editluong frame = new Editluong();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public Editluong(Connection objConnection, UserEntitys objSecurity, LuongEntity objLuongEntity) {
		m_objConnection = objConnection;
		m_objSecurityContext = objSecurity;
		m_LuongEntity = objLuongEntity;
		m_objLuongController = new LuongControllers(m_objConnection, m_objSecurityContext);
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 583, 603);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JToolBar toolBar = new JToolBar();
		toolBar.setBounds(0, 0, 434, 53);
		contentPane.add(toolBar);
		
		JButton btnNewButton = new JButton("Cập nhật");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				m_LuongEntity.setLuongCoBan(Integer.parseInt(lCoBan.getText()));
				m_LuongEntity.setLuongThuongThem(Integer.parseInt(lThuong.getText()));
				m_LuongEntity.setTongLuong(Integer.parseInt(tongLuong.getText()));
				boolean bResult=false;
				try {
					bResult = m_objLuongController.ExecOfUpdateElement(m_LuongEntity);
				} catch (Exception e) {
					// TODO: handle exception
					JOptionPane.showMessageDialog(getParent(), "Err: "+e.getMessage());
				}
				if(bResult){
					JOptionPane.showMessageDialog(getParent(), "cập nhật thành công!!!");
				}
				dispose();
			}
		});
		btnNewButton.setIcon(new ImageIcon(Editluong.class.getResource("/images/13.png")));
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 16));
		toolBar.add(btnNewButton);
		
		JButton btnng = new JButton("Đóng");
		btnng.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnng.setIcon(new ImageIcon(Editluong.class.getResource("/images/6_3.png")));
		btnng.setFont(new Font("Cambria", Font.PLAIN, 16));
		toolBar.add(btnng);
		
		JLabel lblNewLabel = new JLabel("Lương cơ bản:");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 16));
		lblNewLabel.setBounds(33, 363, 115, 37);
		contentPane.add(lblNewLabel);
		
		JLabel lblLngThng = new JLabel("Lương thưởng:");
		lblLngThng.setFont(new Font("Cambria", Font.PLAIN, 16));
		lblLngThng.setBounds(33, 429, 115, 37);
		contentPane.add(lblLngThng);
		
		JLabel lblTngLng = new JLabel("Tổng lương:");
		lblTngLng.setFont(new Font("Cambria", Font.PLAIN, 16));
		lblTngLng.setBounds(33, 497, 115, 37);
		contentPane.add(lblTngLng);
		
		lCoBan = new JTextField();
		lCoBan.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				tongLuong.setText(""+(Integer.parseInt(lCoBan.getText())+Integer.parseInt(lThuong.getText())));
			}
		});
		lCoBan.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lCoBan.setBounds(180, 360, 204, 42);
		contentPane.add(lCoBan);
		lCoBan.setColumns(10);
		
		lThuong = new JTextField();
		lThuong.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				tongLuong.setText(""+(Integer.parseInt(lCoBan.getText())+Integer.parseInt(lThuong.getText())));
			}
		});
		lThuong.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lThuong.setColumns(10);
		lThuong.setBounds(180, 427, 204, 40);
		contentPane.add(lThuong);
		
		tongLuong = new JTextField();
		tongLuong.setFont(new Font("Tahoma", Font.PLAIN, 16));
		tongLuong.setEditable(false);
		tongLuong.setColumns(10);
		tongLuong.setBounds(180, 495, 204, 40);
		contentPane.add(tongLuong);
		
		MaNhanVien = new JTextField();
		MaNhanVien.setEditable(false);
		MaNhanVien.setBounds(180, 83, 204, 37);
		contentPane.add(MaNhanVien);
		MaNhanVien.setColumns(10);
		
		txtHoVaTen = new JTextField();
		txtHoVaTen.setEditable(false);
		txtHoVaTen.setBounds(180, 141, 204, 37);
		contentPane.add(txtHoVaTen);
		txtHoVaTen.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Mã Nhân Viên:");
		lblNewLabel_1.setFont(new Font("Cambria", Font.PLAIN, 16));
		lblNewLabel_1.setBounds(33, 83, 115, 37);
		
		contentPane.add(lblNewLabel_1);
		
		HoVaTen = new JLabel("Họ Và Tên:");
		HoVaTen.setFont(new Font("Cambria", Font.PLAIN, 16));
		
		HoVaTen.setBounds(33, 141, 115, 37);
		contentPane.add(HoVaTen);
		
		txtTrinhDo = new JTextField();
		txtTrinhDo.setEditable(false);
		txtTrinhDo.setBounds(180, 218, 204, 37);
		contentPane.add(txtTrinhDo);
		txtTrinhDo.setColumns(10);
		
		TrinhDo = new JLabel("Trình Độ:");
		TrinhDo.setFont(new Font("Cambria", Font.PLAIN, 16));
		
		TrinhDo.setBounds(33, 218, 115, 37);
		contentPane.add(TrinhDo);
		
		ngayLamViec = new JLabel("Ngày Làm Việc:");
		ngayLamViec.setFont(new Font("Cambria", Font.PLAIN, 16));
		ngayLamViec.setBounds(33, 287, 115, 37);
		contentPane.add(ngayLamViec);
		
		txtNgayLamViec = new JTextField();
		txtNgayLamViec.setEditable(false);
		txtNgayLamViec.setBounds(180, 287, 204, 37);
		contentPane.add(txtNgayLamViec);
		txtNgayLamViec.setColumns(10);
		BindingControl(m_LuongEntity);
	}
	
	public void BindingControl(LuongEntity objLuongEntity){
		MaNhanVien.setText(""+objLuongEntity.getMaNhanVien());
		txtHoVaTen.setText(""+objLuongEntity.getHoVaTen());
		txtTrinhDo.setText(""+objLuongEntity.getTrinhDo());
		txtNgayLamViec.setText(""+objLuongEntity.getNgayLamViec());
		lCoBan.setText(""+objLuongEntity.getLuongCoBan());
		lThuong.setText(""+objLuongEntity.getLuongThuongThem());
		tongLuong.setText(""+objLuongEntity.getTongLuong());
	}
}
